package org.mock.base;

import com.alibaba.fastjson2.JSONObject;

import java.util.function.Function;

public class LambdaRequestHandler<T, R> implements RequestHandler<T, R> {

    private final Function<JSONObject, T> parseRequestFunc;
    private final Function<T, T> buildQueryWrapperFunc;
    private final Function<T, R> fetchDataFunc;
    private final Function<R, String> processResultFunc;

    public LambdaRequestHandler(
            Function<JSONObject, T> parseRequestFunc,
            Function<T, T> buildQueryWrapperFunc,
            Function<T, R> fetchDataFunc,
            Function<R, String> processResultFunc
    ) {
        this.parseRequestFunc = parseRequestFunc;
        this.buildQueryWrapperFunc = buildQueryWrapperFunc;
        this.fetchDataFunc = fetchDataFunc;
        this.processResultFunc = processResultFunc;
    }

    @Override
    public T parseRequest(JSONObject request) {
        return parseRequestFunc.apply(request);
    }

    @Override
    public T buildQueryWrapper(T dto) {
        return buildQueryWrapperFunc.apply(dto);
    }

    @Override
    public R fetchData(T queryWrapper) {
        return fetchDataFunc.apply(queryWrapper);
    }

    @Override
    public String processResult(R result) {
        return processResultFunc.apply(result);
    }
}
